//
//  ChangeContentView.m
//  TeamInformatization
//
//  Created by bigger on 2019/3/31.
//  Copyright © 2019年 SPIC. All rights reserved.
//

#import "ChangeContentView.h"

@interface ChangeContentView ()

// 数据
@property (nonatomic, strong) NSArray *dataArray;

// 指示条
@property (nonatomic, strong) UIView *indicateView;

@end

@implementation ChangeContentView


- (instancetype)initWithFrame:(CGRect)frame TitleArray:(NSArray *)titleArray
{
    if (self = [super initWithFrame:frame]) {
        
        self.dataArray = [NSMutableArray arrayWithArray:titleArray];
        
        [self creatViews];
    }
    
    return self;
}

- (void)creatViews
{
    float buttonWidth = self.frame.size.width / (float)self.dataArray.count;
    
    for (int i = 0; i < _dataArray.count; i++) {
        UIButton *changeButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [changeButton setBackgroundColor:[UIColor whiteColor]];
        [changeButton setTitleColor:kSetRGBColor(102, 102, 102, 1) forState:UIControlStateNormal];
        [changeButton setTitleColor:kCommonRedColor forState:UIControlStateSelected];
        changeButton.titleLabel.font = [UIFont systemFontOfSize:14];
        [changeButton setTitle:_dataArray[i] forState:UIControlStateNormal];
        [changeButton addTarget:self action:@selector(changeContentAction:) forControlEvents:UIControlEventTouchUpInside];
        changeButton.selected = NO;
        changeButton.tag = 1100 + i;
        if (i == 0) {
            changeButton.titleLabel.font = [UIFont boldSystemFontOfSize:16];
            changeButton.selected = YES;
        }
        [self addSubview:changeButton];
        
        [changeButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(- 1 * kScaleWidth);
            make.left.mas_equalTo(buttonWidth * i);
            make.top.mas_equalTo(0);
            make.width.mas_equalTo(buttonWidth);
        }];
    }
    
    self.indicateView = [[UIView alloc] init];
    _indicateView.backgroundColor = kCommonRedColor;
    [self addSubview:_indicateView];
    [_indicateView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-2 * kScaleWidth);
        make.left.mas_equalTo(10 * kScaleWidth);
        make.size.mas_equalTo(CGSizeMake(buttonWidth - 20 * kScaleWidth, 2 * kScaleWidth));
    }];
    
    
}

- (void)changeContentAction:(UIButton *)sender
{
    for (int i = 0; i < _dataArray.count; i++) {
        UIButton *button = [self viewWithTag:(1100 + i)];
        button.selected = NO;
        button.titleLabel.font = [UIFont systemFontOfSize:14];
    }
    
    sender.selected = YES;
    sender.titleLabel.font = [UIFont boldSystemFontOfSize:16];
    
    float buttonWidth = self.frame.size.width / (float)self.dataArray.count;
    
    [_indicateView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(buttonWidth * (sender.tag - 1100) + 10 * kScaleWidth);
    }];
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(getTouchNum: withChangeContentView:)]) {
        //代理存在且有这个transButIndex:方法
        [self.delegate getTouchNum:(int)(sender.tag - 1100)  withChangeContentView:self];
    }
    
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
